/*
 * @Descripttion: 
 * @Author: chendingfengmail@163.com
 * @version: 
 * @Date: 2021-06-15 07:59:24
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2022-01-17 13:56:54
 */
import api from '@/api'

export default {
  namespaced: true,
  state: {
    // 角色
    roles: []
  },
  actions: {

    /**
     * 加载角色列表
     * @param {*} isLoadInCache 是否从缓存加载
     */
     loadRoles({ commit, dispatch }, isLoadInCache) {
      if (isLoadInCache) {
        dispatch('load');
      } else {
        dispatch('getRolesList');
      }
    },

    // 获取角色列表
    getRolesList({ commit, dispatch }) {
      let params = {
        pageIndex: 1,
        pageSize: 999
      }
      return new Promise((resolve, reject) => {
        api.userRole.findRoleList(params).then((res) => {
          dispatch('set', res.value);
          resolve(res.value);
        }).catch((error) => {
          console.log(error)
          reject(error)
        })
      })
    },

    /**
     * @description 设置角色数据
     * @param {Object} context
     * @param {*} roles roles
     */
    async set({ state, dispatch }, roles) {
      // store 赋值
      state.roles = roles
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'zhgd',
        path: 'roles',
        value: roles,
        user: true
      }, { root: true })
    },
    /**
     * @description 从数据库取角色数据
     * @param {Object} context
     */
    async load({ state, dispatch }) {
      // store 赋值
      state.roles = await dispatch('ddAdmin/db/get', {
        dbName: 'zhgd',
        path: 'roles',
        defaultValue: {},
        user: true
      }, { root: true })
    }
  }
}
